﻿using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Epitech.Krisk.Xna.ScreenManagerLib;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;
using System;

namespace Epitech.Krisk.Xna.UI.Game.Menu
{
    class EndGameView : MenuScreen
    {
        RenderBackground _bg;
        GamePlay _gamePlay;
        Texture2D _star;

        public EndGameView(GamePlay gamePlay)
            : base("Partie terminee !")
        {
            // Create our menu entries.
            _gamePlay = gamePlay;
            LayoutEntry Menu = new LayoutEntry(new Vector2(1575f, 900f));
            Menu.Alignement = LayoutEntry.TextAlignement.Right;
            MenuEntry mainMenuEntry = new MenuEntry("[ Menu principal ]");
            //MenuEntry quitEntry = new MenuEntry("[ Quitter ]");

            mainMenuEntry.Selected += mainMenu;
            //quitEntry.Selected += OnCancel;

            Menu.MenuEntries.Add(mainMenuEntry);
            //Menu.MenuEntries.Add(quitEntry);
            // Add entries to the menu.
            MenuEntries.Add(Menu);



            foreach (MenuEntry item in Menu.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _bg.Draw(gameTime);

            this.printText("Joueur", 350, 150, new Color(0,0,0));
            this.printText("Armees", 700, 150, new Color(0,0,0));
            this.printText("Territoires", 1050, 150, new Color(0,0,0));
            this.printText("Duree de vie", 1400, 150, new Color(0,0,0));

            int playerPos = 250;
            Color _color;
            foreach (var item in _gamePlay.Players)
	        {
                if (item.Win || (item.State == PlayerState.Alive && _gamePlay.GameType == GamePlayType.Domination))
                    _color = new Color(208, 44, 6);
                else
                    _color = new Color(255, 255, 255);
                this.printText(item.Name, 350, playerPos, _color);
                this.printText(item.TotalArmy.ToString(), 700, playerPos, _color);
                this.printText(item.OwnTerritory.Count.ToString(), 1050, playerPos, _color);
                string life = item.TimeToDeath.ToString("g");
                if (item.TimeToDeath == TimeSpan.Zero)
                    life = "Joueur vivant";
                this.printText(life, 1400, playerPos, _color);
                if (item.Win || (item.State == PlayerState.Alive && _gamePlay.GameType == GamePlayType.Domination))
                {
                    ScreenManager.SpriteBatch.Begin();
                    ScreenManager.SpriteBatch.Draw(_star, new Rectangle(230, playerPos - (_star.Height / 2) - 10, _star.Width / 2, _star.Height / 2), Color.White);
                    ScreenManager.SpriteBatch.End();
                }
                playerPos += 150;
            }
            base.Draw(gameTime);
        }

        public override void LoadContent()
        {
            _bg = new RenderBackground("Game/Background");
            _bg.Content = ScreenManager.Game.Content;
            _bg.SpriteBatch = ScreenManager.SpriteBatch;
            _bg.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _bg.LoadContent();

            _star = ScreenManager.Game.Content.Load<Texture2D>("Game/Star");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        private void printText(string text, float x, float y, Color color)
        {
            SpriteBatch sp = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            sp.Begin();

            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(x, y);
            Vector2 titleOrigin = font.MeasureString(text) / 2;
            Color titleColor = color * TransitionAlpha;
            float titleScale = 0.8f;

            titlePosition.Y -= transitionOffset * 100;

            sp.DrawString(font, text, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            sp.End();
        }

        #region Handle Input


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.Game.Exit();
        }

        protected void mainMenu(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            Music.GetInstance().setMusic("Music/IntroMusic");
            ScreenManager.AddScreen(new MainMenu(), null);
        }

        #endregion
    }
}
